package com.cyan.app.workflow;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.cyan.app.workflow.util.TelephonyUtil;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

public class AppContext extends Application {
    private static AppContext singleInstance;

    public static AppContext getInstance() {
        return singleInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        singleInstance = this;

        initVolley();
        initVersion();
        initDeviceInfo();
        initImageLoader(this);
    }

    private void initVolley() {
        VolleyUtil.getInstance().init(this);
    }

    public void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        File cacheDir = initPicPath();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(1080, 1920)
                .threadPoolSize(3)
                .threadPriority(Thread.NORM_PRIORITY - 1)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .discCache(new UnlimitedDiscCache(cacheDir))
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileCount(100)
                .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000))
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    public File initPicPath() {
        File picTmpDir = StorageUtils.getOwnCacheDirectory(
                getApplicationContext(), "ACE/Cache");
        if (!picTmpDir.exists()) {
            picTmpDir.mkdirs();
        }
        return picTmpDir;
    }

    public void initDeviceInfo() {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return;
        }
        DisplayMetrics metric = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metric);
        AppData.getInstance().setScreenWidth(metric.widthPixels);
        AppData.getInstance().setScreenHeight(metric.heightPixels);
        AppData.UUID = new TelephonyUtil(this).getIme();
    }

    public void initVersion() {
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            AppData.APPVERSION = packageInfo.versionName;
            AppData.APPVERSIONCODE = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
        }
    }
}